﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class CityMaster : MasterBase
    {
        public ResultInfo GetAllItems(string provinceCode, string key,int index,int size)
        {
            ResultInfo result = new ResultInfo(ResultValue.Succeed);

            int count = 0;

            DataTable table = GetInstance<CityService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_BASE), provinceCode, key, index, size, ref count);

            string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

            var items = (from t in table.AsEnumerable()
                         select new DataObjectInfo()
                         {
                             Values = GetObjectOfValue(t, columns)
                         });

            result.DataObjects.AddRange(items);
            result.TotalCount = count;

            return result;
        }

        public ResultInfo GetAllItems(string provinceCode)
        {
            var items = GetInstance<CityService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_DATA), provinceCode);
            ResultInfo result = new ResultInfo(ResultValue.Succeed);
            result.Entities.AddRange(items);
            return result;
        }

        public ResultInfo Update(string account, string password, CityInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<CityService>().Update(ConnectionString(DatabaseKeys.CORINA_BASE), entity, operation);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo GetNewEntity(string account, string password, string provinceCode)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                string code = GetInstance<CityService>().GetCode(ConnectionString(DatabaseKeys.CORINA_BASE), provinceCode);

                CityInfo entity = new CityInfo() { Code = code };

                result.Entities.Add(entity);
            }

            return result;
        }

        public ResultInfo GetCityInfo(string account,string password,int id)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                CityInfo entity = GetInstance<CityService>().GetCityInFo(ConnectionString(DatabaseKeys.CORINA_BASE), id);

                result.Entities.Add(entity);
            }

            return result;
        }
    }
}
